package com.example.myapplication;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * create an instance of this fragment.
 */
public class SettingFragment extends Fragment implements View.OnClickListener {
    ImageButton imageButton_link;
    ImageButton imageButton_end;
    EditText editText_ipaddress;
    EditText editText_comnumber;
    TextView textView_status;
    String ipaddress="192.168.137.66";
    String port="8888";
    public SettingFragment.cmdReceiver cmdReceiver;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View CtrlLayyout=inflater.inflate(R.layout.fragment_setting,container,false);
        imageButton_link=CtrlLayyout.findViewById(R.id.imageButton_link);
        imageButton_link.setOnClickListener(this);
        imageButton_end=CtrlLayyout.findViewById(R.id.imageButton_end);
        imageButton_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO Auto-generated method stub
                System.out.println("程序关闭");
                showNormalDialog("确定退出程序吗？",2);
            }
        });
        editText_ipaddress=CtrlLayyout.findViewById(R.id.editText_ipaddress);
        editText_comnumber=CtrlLayyout.findViewById(R.id.editText_comnumber);
        textView_status=CtrlLayyout.findViewById(R.id.textView_status);
        //注册广播接收器
        cmdReceiver =new SettingFragment.cmdReceiver();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("com.example.communication.data");
        getActivity().registerReceiver(cmdReceiver,intentFilter);

        return CtrlLayyout;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.imageButton_link:
            {
                ipaddress=editText_ipaddress.getText().toString();
                port=editText_comnumber.getText().toString();
                textView_status.setText("IP地址："+ipaddress+"，端口号："+port+"正在连接。。。");

                ///开始连接新的TCP
                Intent mIntent = new Intent(getActivity(), TCPService.class);
                mIntent.putExtra("ipaddress", ipaddress);
                mIntent.putExtra("port", port);
                getActivity().startService(mIntent);

            }break;
        }
    }
    private void showNormalDialog(String strtip,int num){
        final int a=num;
        //创建dialog构造器
        androidx.appcompat.app.AlertDialog.Builder normalDialog = new AlertDialog.Builder(getContext());
        //设置title
        normalDialog.setTitle("温馨提示：");
        //设置icon
        normalDialog.setIcon(R.drawable.appicon);
        //设置内容
        normalDialog.setMessage(strtip);
        //设置按钮
        normalDialog.setPositiveButton(getString(R.string.sure)
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("确定");
                        if(a>1)
                        {
                            TCPService.send("Disconnect!".getBytes(StandardCharsets.UTF_8));
                            for(int i=0;i<1000;i++);
                            getActivity().finish();//关闭Activity
                            System.exit(0);//退出APP
                        }

                    }
                });
        if(a==2)
            normalDialog.setNegativeButton(getString(R.string.cancel)
                    , new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            System.out.println("取消");
                        }
                    });
        //创建并显示
        normalDialog.create().show();
    }
    private class cmdReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            String recieveData=intent.getStringExtra("data");
            if(recieveData!=null)
            {
                if(recieveData.equals("Connected!"))
                {
                    showNormalDialog("接入成功",0);
                    textView_status.setText("IP地址："+ipaddress+"，端口号："+port+"接入成功");
                }
            }
        }
    }

}
